<template>
  <!-- 容器 -->
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header
      ><div>
        <img src="../assets/MM.jpg" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout()">退出</el-button>
    </el-header>

    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside :width="isCollapse ? '64px' : '200px'"
        ><div class="switch-button" @click="collapse()">||||</div>
        <el-menu
          background-color="#545c64"
          text-color="#fff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 一级目录 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <!-- 一级目录和图标 -->
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>

            <!-- 二级目录 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="SaveNavState('/' + subItem.path)"
            >
              <!-- 二级目录和图标 -->
              <i class="el-icon-menu"></i>
              <span slot="title">{{ subItem.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容区域 -->
      <el-main>
        <!-- 路由占位符，显示mian里面的内容 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      /* 初始化菜单列表为空 */
      menulist: [],
      iconsObj: {
        125: "el-icon-s-check",
        103: "el-icon-box",
        101: "el-icon-goods",
        102: "el-icon-s-flag",
        145: "el-icon-data-line",
      },

      /*菜单默认不折叠  */
      isCollapse: false,
      /* 把激活状态的初始值设为空 */
      activePath: "",
    };
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem("activePath");
  },
  methods: {
    logout() {
      // 清空token
      window.sessionStorage.clear();
      // 重定向到主页面/login
      this.$router.push("/login");
    },
    /* 获取所有菜单 */
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status !== 200) {
        return this.$message.console.error(res.meta.msg);
      } else {
        this.menulist = res.data;
      }
      console.log(res);
    },

    /* 点击之后折叠菜单（刚开始菜单为不折叠） */
    collapse() {
      this.isCollapse = !this.isCollapse;
    },

    /*将激活状态保存在sessionStorage中  */
    SaveNavState(activePath) {
      /* 将组件页面路径赋值给essionStorage */
      window.sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;
    },
  },
};
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #bcb6b1;
  display: flex;
  //靠向左右
  justify-content: space-between;
  padding-left: 0px;
  padding-right: 10px;
  align-items: center;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 10px;
      font-size: 20px;
    }
  }
}

.el-aside {
  background-color: #545c64;
  > .el-menu {
    border-right: none;
  }
  .switch-button {
    color: white;
    text-align: center;
    font-size: 15px;
    line-height: 24px;
    /* 设置字符间距 */
    letter-spacing: 0.2em;
    /* 鼠标放上去有小手 */
    cursor: pointer;
  }
}
.el-main {
  background-color: white;
}
</style>
